import os

from google.appengine.ext import db
from google.appengine.ext.webapp import template

from model import Language, Settings, Contact
from model import Category, Wiki
from model import Author, Exposition, Picture

from tools import manager

class LanguageManager(manager.Manager):
    """
    """

    def get_language(self, language_key):
        # get asked language
        if language_key and not language_key == '':
            return Language().get(language_key)
        else:
            return ''

    def edit(self, language_key=None, locale_id=None):
        """
        """
        # get asked language from get/post parameter
        if locale_id and not locale_id == '':
            language_to_edit = self.get_language_from_id(locale_id)
        # get asked language from post
        elif language_key and not language_key == '':
            language_to_edit = Language().get(language_key)
        else:
            language_to_edit = self.get_default_language()

        # set the template for edit
        self.template_values['language_to_edit'] = language_to_edit

        # find the template
        path = os.path.join(os.path.dirname(__file__),
                            '../templates', 'language', 'edit.html')

        # display the contact
        return template.render(path, self.template_values)

    def new_language(self):
        """
        """
        # create a default language
        language = Language()

        language.locale_id = db.Category(' ')

        # default current false
        language.default = False

        # db update
        language.put()

        # key return
        return language.key()

    def save(self, language_key, locale_id, name, default):
        """
        """
        if default:
            # get all
            query = Language().all()

            # disable all defaults
            for language in query:
                language.default = False
                language.put()

        #
        language = Language().get(language_key)

        # set the id
        locale_id = locale_id.strip()
        # can't be empty
        if locale_id == '':
            locale_id = ' '

        # set
        language.locale_id = db.Category(locale_id)
        # set the title
        language.name = name
        # set default
        language.default = default
        # db update
        language.put()
        # return at the end (useful)
        return language

    def delete(self, language_key):
        """
        """
        # language to delete
        asked_language = Language().get(language_key)

        # if different than the current we can delete it
        if not self.language.key() == asked_language.key():

            query = Category().all()
            query.filter('language =', asked_language)
            if query.count() > 1:
                return
            if query.count() == 1:
                query.get().delete()

            query = Wiki().all()
            query.filter('language =', asked_language)
            if query.count() > 1:
                return
            if query.count() == 1:
                query.get().delete()

            query = Author().all()
            query.filter('language =', asked_language)
            if query.count() > 1:
                return
            if query.count() == 1:
                query.get().delete()

            query = Exposition().all()
            query.filter('language =', asked_language)
            if query.count() > 1:
                return
            if query.count() == 1:
                query.get().delete()

            query = Picture().all()
            query.filter('language =', asked_language)
            if query.count() > 1:
                return
            if query.count() == 1:
                query.get().delete()

            query = Settings().all()
            query.filter('language =', asked_language)
            if query.count() > 0:
                query.get().delete()

            query = Contact().all()
            query.filter('language =', asked_language)
            if query.count() > 0:
                query.get().delete()

            # set another default
            if asked_language.default:
                self.language.default = True
                self.language.put()

            # delete from db
            asked_language.delete()
